package com.wk.contrller;

import com.wk.bean.PageInfo;
import com.wk.bean.Patient;
import com.wk.bean.WkUser;
import com.wk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/auth") // 这里添加了 /auth 前缀
@CrossOrigin(origins = "http://localhost:8080")
public class PatientController {
    @Autowired
    UserService userService;
    //患者分页查询
    @PostMapping("/listPatientWithPaginationa")
    public ResponseEntity<Map<String, Object>> listPatientWithPaginationa(@RequestBody Map<String, Object> queryParams) {
        int page = Integer.parseInt(queryParams.get("page").toString());
        int pageSize = Integer.parseInt(queryParams.get("pageSize").toString());

        PageInfo pageInfo = new PageInfo();
        pageInfo.setOffset((page - 1) * pageSize);
        pageInfo.setPageSize(pageSize);

        List<Patient> users = userService.listPatientWithPaginationa(pageInfo);

        Map<String, Object> response = new HashMap<>();
        if (users != null) {
            response.put("message", "成功获取用户列表。");
            response.put("users", users);
            return new ResponseEntity<>(response, HttpStatus.OK);
        } else {
            response.put("message", "未找到用户信息。");
            return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
        }
    }
    // 删除患者
    @PostMapping("/deletePatient")
    public ResponseEntity<String> deletePatient(@RequestBody Map<String, Object> deleteParams) {
        int patientIdToDelete = Integer.parseInt(deleteParams.get("patientId").toString()); // 获取要删除的患者ID
        int result = userService.deletePatient(patientIdToDelete);

        if (result == 0) {
            // 删除成功，返回适当的成功消息
            return new ResponseEntity<>("删除成功", HttpStatus.OK);
        } else {
            // 删除失败或用户不存在，返回适当的错误信息
            return new ResponseEntity<>("删除失败.", HttpStatus.BAD_REQUEST);
        }
    }

//    // 更新用户信息，包括密码
//    @PostMapping("/modifypatient")
//    public ResponseEntity<String> updateUser(@RequestBody Map<String, Object> updateParams) {
//        int patientId = Integer.parseInt(updateParams.get("patientId").toString());
//        String newPatientName = (String) updateParams.get("newPatientName");
//        String newAge = (String) updateParams.get("newAge");
//        String newgGnder = (String) updateParams.get("newgGnder");
//
//        // 通过用户ID检索用户
//        Patient patient = userService.queryUserById(patientId);
//
//        if (patient == null) {
//            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("用户不存在");
//        }
//
//        // 更新患者信息
//        patient.setPatientName(newPatientName);
//        patient.setAge(newAge);
//        patient.setGnder(newgGnder);
//
//        int result = userService.updateUser(patient);
//
//        if (result > 0) {
//            return ResponseEntity.ok("用户信息更新成功");
//        } else {
//            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("用户信息更新失败");
//        }
//    }

    //查找
    @PostMapping("/queryPatient")
    public ResponseEntity<?> queryPatient(@RequestBody Map<String, Object> queryParams) {
        try {
            Patient user = userService.queryPatientByFields(queryParams);
            if (user != null) {
                return new ResponseEntity<>(user, HttpStatus.OK);
            } else {
                return new ResponseEntity<>("未找到用户信息。", HttpStatus.NOT_FOUND);
            }
        } catch (Exception e) {
            return new ResponseEntity<>("查询用户失败。", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
